package com.canaan.business.service.inOutStock.impl;

import com.canaan.business.domain.basic.WmsProduct;
import com.canaan.business.domain.inOutStock.StockOccupyDetail;
import com.canaan.business.domain.vo.StockOccupyDetailVo;
import com.canaan.business.mapper.basic.WmsProductMapper;
import com.canaan.business.mapper.inOutStock.StockOccupyDetailMapper;
import com.canaan.business.service.inOutStock.IStockOccupyDetailService;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.StringUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.bean.BeanUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 库存占用明细Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-11
 */
@Service
public class StockOccupyDetailServiceImpl implements IStockOccupyDetailService {
    @Resource
    private StockOccupyDetailMapper stockOccupyDetailMapper;

    @Resource
    private WmsProductMapper wmsProductMapper;
    /**
     * 查询库存占用明细
     *
     * @param id 库存占用明细主键
     * @return 库存占用明细
     */
    @Override
    public StockOccupyDetail selectStockOccupyDetailById(Long id) {
        return stockOccupyDetailMapper.selectStockOccupyDetailById(id);
    }

    /**
     * 查询库存占用明细列表
     *
     * @param stockOccupyDetail 库存占用明细
     * @return 库存占用明细
     */
    @Override
    public List<StockOccupyDetail> selectStockOccupyDetailList(StockOccupyDetail stockOccupyDetail) {
        return stockOccupyDetailMapper.selectStockOccupyDetailList(stockOccupyDetail);
    }

    /**
     * 查询库存占用明细列表
     *
     * @param stockOccupyDetail 库存占用明细
     * @return 库存占用明细
     */
    @Override
    public List<StockOccupyDetailVo> selectStockOccupyDetailVoList(StockOccupyDetail stockOccupyDetail) {
        List<StockOccupyDetailVo> detailVos = Lists.newArrayList();

        List<StockOccupyDetail> stockOccupyDetailList = stockOccupyDetailMapper.selectStockOccupyDetailList(stockOccupyDetail);
        List<String> productNos = stockOccupyDetailList.stream().map(StockOccupyDetail::getProductNo).collect(Collectors.toList());

        if(!productNos.isEmpty()){
            List<WmsProduct> products = wmsProductMapper.selectWmsProductsByProductNos(productNos);
            Map<String, String> productMap = products.stream().collect(Collectors.toMap(WmsProduct::getProductNo,WmsProduct::getProductName));

            for (StockOccupyDetail detail : stockOccupyDetailList){
                StockOccupyDetailVo vo = new StockOccupyDetailVo();
                BeanUtils.copyProperties(detail,vo);
                if(productMap.containsKey(detail.getProductNo())){
                    vo.setProductName(productMap.get(detail.getProductNo()));
                }
                detailVos.add(vo);
            }
        }
        return detailVos;
    }

    /**
     * 新增库存占用明细
     *
     * @param stockOccupyDetail 库存占用明细
     * @return 结果
     */
    @Override
    public int insertStockOccupyDetail(StockOccupyDetail stockOccupyDetail) {
        if(StringUtils.isBlank(stockOccupyDetail.getNo())){
            stockOccupyDetail.setNo(TableNoUtil.getNo(StockOccupyDetail.class));
        }
        stockOccupyDetail.setCreateBy(SecurityUtils.getUsername());
        stockOccupyDetail.setCreateTime(DateUtils.getNowDate());
        return stockOccupyDetailMapper.insertStockOccupyDetail(stockOccupyDetail);
    }

    /**
     * 批量新增库存占用明细
     *
     * @param stockOccupyDetailList 库存占用明细列表
     * @return 结果
     */
    @Override
    public int batchInsertStockOccupyDetail(List<StockOccupyDetail> stockOccupyDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StockOccupyDetail stockOccupyDetail : stockOccupyDetailList){
            if(StringUtils.isBlank(stockOccupyDetail.getNo())){
                stockOccupyDetail.setNo(TableNoUtil.getNo(StockOccupyDetail.class));
            }
            stockOccupyDetail.setCreateBy(username);
            stockOccupyDetail.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<stockOccupyDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > stockOccupyDetailList.size()) {
                endIndex = stockOccupyDetailList.size();
            }
            insertNum = insertNum + stockOccupyDetailMapper.batchInsertStockOccupyDetail(stockOccupyDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改库存占用明细
     *
     * @param stockOccupyDetail 库存占用明细
     * @return 结果
     */
    @Override
    public int updateStockOccupyDetail(StockOccupyDetail stockOccupyDetail) {
        stockOccupyDetail.setUpdateBy(SecurityUtils.getUsername());
        stockOccupyDetail.setUpdateTime(DateUtils.getNowDate());
        return stockOccupyDetailMapper.updateStockOccupyDetail(stockOccupyDetail);
    }

    /**
     * 批量修改库存占用明细
     *
     * @param stockOccupyDetailList 库存占用明细列表
     * @return 结果
     */
    @Override
    public int batchUpdateStockOccupyDetail(List<StockOccupyDetail> stockOccupyDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StockOccupyDetail stockOccupyDetail : stockOccupyDetailList){
            stockOccupyDetail.setUpdateBy(username);
            stockOccupyDetail.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<stockOccupyDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > stockOccupyDetailList.size()) {
                endIndex = stockOccupyDetailList.size();
            }
            updateNum = updateNum + stockOccupyDetailMapper.batchUpdateStockOccupyDetail(stockOccupyDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除库存占用明细
     *
     * @param ids 需要删除的库存占用明细主键集合
     * @return 结果
     */
    @Override
    public int deleteStockOccupyDetailByIds(Long[] ids) {
        return stockOccupyDetailMapper.deleteStockOccupyDetailByIds(ids);
    }

    /**
     * 删除库存占用明细信息
     *
     * @param id 库存占用明细主键
     * @return 结果
     */
    @Override
    public int deleteStockOccupyDetailById(Long id) {
        return stockOccupyDetailMapper.deleteStockOccupyDetailById(id);
    }

    /**
     * 查询库存占用明细
     *
     * @param no 编号
     * @return 库存占用明细
     */
    @Override
    public StockOccupyDetail selectStockOccupyDetailByNo(String no) {
        return stockOccupyDetailMapper.selectStockOccupyDetailByNo(no);
    }

    /**
     * 批量删除库存占用明细
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteStockOccupyDetailByNos(String[] nos) {
        return stockOccupyDetailMapper.deleteStockOccupyDetailByNos(nos);
    }

    /**
     * 删除库存占用明细信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteStockOccupyDetailByNo(String no) {
        return stockOccupyDetailMapper.deleteStockOccupyDetailByNo(no);
    }

    @Override
    public List<StockOccupyDetail> getStockOccupyDetailByStorehouseNoProductNo(String storehouseNo, String productNo, String occupyType) {
        return stockOccupyDetailMapper.getStockOccupyDetailByStorehouseNoProductNo(storehouseNo,productNo,occupyType);
    }
}
